// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
const _ = db.command
const $ = db.command.aggregate

// 云函数入口函数
exports.main = async (event, context) => {
  // const wxContext = cloud.getWXContext()
  // openid: wxContext.OPENID,

  if (event.action == 'getTabs') {
    return await db.collection('classOrder').aggregate()
      .match({
        date: event.date,
        status: '待使用'
      })
      .group({
        _id: '$venueFloor',
        num: $.sum(1)
      })
      .sort({
        _id: 1
      })
      .end()
  } else if (event.action == 'adminOrder') {
    return await db.collection('classOrder')
      .where({
        status: '待使用',
        venueFloor: event.venueFloor,
        date: event.date
      })
      .get()
  }
}